﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;
using System.Windows.Forms;

namespace Net_SCADA
{
    class fd_PDF
    {
        public PdfDocument Doc { get; set; }
        public PdfPageBase Page { get; private set; }
        public PdfGrid Grid { get; private set; }
        public PdfGridRow GridRow { get; set; }
        public float PageWidth { get { return Doc.PageSettings.Width; } }
        public float PageHeight { get { return Doc.PageSettings.Height; } }
        public float Margin = 20;



        public fd_PDF()
        {
            Doc = new PdfDocument();
            Doc.PageSettings.Margins.All = 0;//设置边距为0，默认边距为40
            Doc.PageSettings.Orientation = PdfPageOrientation.Portrait;
            PagesAdd(); PageRemove(Page);//删除第一页，因为第一页有警告语
        }

        public fd_PDF(PdfPageOrientation orientation)
        {
            Doc = new PdfDocument();
            Doc.PageSettings.Margins.All = 0;//设置边距为0，默认边距为40
            Doc.PageSettings.Orientation = orientation;
            PagesAdd(); PageRemove(Page);//删除第一页，因为第一页有警告语
        }

        public PdfPageBase PagesAdd()
        {
            return Page = Doc.Pages.Add();
        }
        public void PageRemove(PdfPageBase page)
        {
            Doc.Pages.Remove(page);
        }

        public PdfTrueTypeFont Font(float emSize, bool IsBold = false)
        {
            return new PdfTrueTypeFont(new Font("宋体", emSize, IsBold ? FontStyle.Bold : FontStyle.Regular), true);//true防止中文乱码
        }
        public PdfSolidBrush BrushColor(Color color)
        {
            return new PdfSolidBrush(color);
        }

        public PdfPen PenColor(Color color, float width = 1)
        {
            return new PdfPen(color, width);
        }
        public bool SaveToPDF(string filename)
        {
            try
            {
                //string path = Application.StartupPath + "\\Reports\\" + name + ".pdf";//以日期为文件名，以防存在打开文件
                Doc.SaveToFile(filename);
                Doc.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }



        public void DrawString(string s, PdfTrueTypeFont font, PdfSolidBrush brush, RectangleF layoutRectangle, PdfPageBase page = null, PdfTextAlignment alignment = PdfTextAlignment.Left)
        {
            (page ?? Page).Canvas.DrawString(s, font, brush, layoutRectangle, new PdfStringFormat(alignment, PdfVerticalAlignment.Middle));
        }
        public void DrawString(string s, PdfTrueTypeFont font, PdfSolidBrush brush, PointF point, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawString(s, font, brush, point);
        }
        public void DrawImage(Image image, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawImage(PdfImage.FromImage(image), rectangle);
        }

        public void DrawLine(PdfPen pen, PointF point1, PointF point2, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawLine(pen, point1, point2);
        }
        public void DrawArc(PdfPen pen, RectangleF rectangle, float startAngle, float sweepAngle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawArc(pen, rectangle, startAngle, sweepAngle);
        }
        public void DrawBezier(PdfPen pen, PointF startPoint, PointF firstControlPoint, PointF secondControlPoint, PointF endPoint, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawBezier(pen, startPoint, firstControlPoint, secondControlPoint, endPoint);
        }


        public void DrawRectangle(PdfBrush brush, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawRectangle(brush, rectangle);
        }

        public void DrawRectangle(PdfPen pen, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawRectangle(pen, rectangle);
        }
        public void DrawRectangle(PdfPen pen, PdfBrush brush, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawRectangle(pen, brush, rectangle);
        }
        public void DrawEllipse(PdfBrush brush, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawEllipse(brush, rectangle);
        }

        public void DrawEllipse(PdfPen pen, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawEllipse(pen, rectangle);
        }


        public void DrawEllipse(PdfPen pen, PdfBrush brush, RectangleF rectangle, PdfPageBase page = null)
        {
            (page ?? Page).Canvas.DrawEllipse(pen, brush, rectangle);
        }

        public void GridInit(float[] colWidths)
        {
            Grid = new PdfGrid();
            Grid.Columns.Add(colWidths.Length);//增加列
            Grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
            Grid.Style.Font = Font(10);//设置字体

            //设置各列宽度
            for (int i = 0; i < colWidths.Length; i++)
            {
                Grid.Columns[i].Width = colWidths[i];
            }
        }


        public PdfTextAlignment[] GridTextAlignments;

        public PdfTrueTypeFont[] GridTextFonts;

        public PdfSolidBrush[] GridTextColors;


        public void GridRowsAdd(float rowHeight = 18f)
        {
            GridRow = Grid.Rows.Add();
            GridRow.Height = rowHeight;//设置行高
            for (int i = 0; i < GridRow.Cells.Count; i++)
            {
                GridRow.Cells[i].StringFormat = new PdfStringFormat(
                    GridTextAlignments == null ? PdfTextAlignment.Left : GridTextAlignments[i], PdfVerticalAlignment.Middle);
            }
        }


        public void SetGridRowCellsValue(string[] values)
        {
            for (int i = 0; i < GridRow.Cells.Count; i++)
            {
                GridRow.Cells[i].Value = values[i];
                GridRow.Cells[i].Style.Font = GridTextFonts == null ? Grid.Style.Font : GridTextFonts[i];
                GridRow.Cells[i].Style.TextBrush = GridTextColors == null ? new PdfSolidBrush(Color.Black) : GridTextColors[i];
            }
        }


        public void GridRowsAdd(params string[] values)
        {
            GridRowsAdd();
            SetGridRowCellsValue(values);
        }

        public void SetBorders()
        {
            PdfBorders borders = new PdfBorders
            {
                All = new PdfPen(Color.Transparent, 0.1f)
            };
            foreach (PdfGridRow row in Grid.Rows)
            {
                foreach (PdfGridCell cell in row.Cells)
                {
                    cell.Style.Borders = borders;
                }
            }
        }

        public void GridDraw(PointF pointf, bool isNoBorder = true, PdfPageBase page = null)//测试改为false
        {
            if (isNoBorder)
                SetBorders();
            Grid.Draw(page ?? Page, pointf);
        }
        /// <summary>
        /// 设置页眉
        /// </summary>
        public void SetPageHeader()
        {
            foreach (var item in Doc.Pages)
            {
                float colWidth = (PageWidth - 2 * 30) / 2;
                GridInit(new float[2] { colWidth, colWidth });
                GridTextAlignments = new PdfTextAlignment[2] { PdfTextAlignment.Left, PdfTextAlignment.Right };
                GridTextFonts = new PdfTrueTypeFont[2] { Font(10), Font(10) };
                GridTextColors = new PdfSolidBrush[2] { BrushColor(Color.Black), BrushColor(Color.Black) };
                GridRowsAdd("Logo", "Text");
                GridDraw(new PointF(30, 20), true, (PdfPageBase)item);

                DrawLine(PenColor(Color.Black), new PointF(30, 40), new PointF(PageWidth - 30, 40), (PdfPageBase)item);
            }
        }
        public SizeF GetTextSize(Font font, string content)
        {
            SizeF sizeF;
            Label alabel = new Label();
            alabel.Text = content;
            Graphics g = alabel.CreateGraphics();
            sizeF = g.MeasureString(alabel.Text, font);
            return sizeF;
        }
        /// <summary>
        /// 设置页脚
        /// </summary>
        public void SetPageFooter()
        {
            int count = 0;
            int allCount = Doc.Pages.Count;//总页数
            foreach (var item in Doc.Pages)
            {
                count++;//当前页码
                DrawLine(PenColor(Color.Black), new PointF(30, PageHeight - 40), new PointF(PageWidth - 30, PageHeight - 40), (PdfPageBase)item);//底线
                float colWidth = (PageWidth - 2 * 30) / 3;
                GridInit(new float[3] { colWidth, colWidth, colWidth });
                GridTextAlignments = new PdfTextAlignment[3] { PdfTextAlignment.Left, PdfTextAlignment.Center, PdfTextAlignment.Right };
                GridTextFonts = new PdfTrueTypeFont[3] { Font(8), Font(10), Font(8) };
                GridTextColors = new PdfSolidBrush[3] { BrushColor(Color.Black), BrushColor(Color.Gray), BrushColor(Color.Black) };
                GridRowsAdd(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "Logo", count + "/" + allCount);
                GridDraw(new PointF(30, PageHeight - 40), true, (PdfPageBase)item);
            }
        }

    }
}
